<!--
 * @Author: wwssaabb
 * @Date: 2021-11-24 10:50:10
 * @LastEditTime: 2021-11-24 14:58:30
 * @FilePath: \handwritten-code\css\垂直居中.html
-->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  * {
    padding: 0;
    margin: 0;
  }

  body {
    width: 100%;
    height: 100vh;
  }

  .section {
    position: relative;
    margin-bottom: 20px;
    background: skyblue;
    height: 200px;
  }

  .item {
    width: 100px;
    height: 100px;
    background-color: seagreen;
    border-radius: 3px;
    text-align: center;
    font-size: 12px;
    color: #fff;
  }

  .margin1 .item {
    /*已知宽高 absolute+margin*/
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -50px;
    margin-top: -50px;
  }

  .margin2 .item {
    /*已知宽高 absolute+margin auto*/
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
  }

  .margin3 .item {
    /*已知宽高 absolute+margin+scale*/
    position: absolute;
    top: calc(50% - 50px);
    left: calc(50% - 50px);
  }

  .position1 .item {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .inline_box1 {
    text-align: center;
    line-height: 200px;
  }

  .inline_box1 .item {
    display: inline-block;
    vertical-align: middle;
  }

  .table_box1 {
    width: 100vw;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
  }

  .table_box1 .item {
    display: inline-block;
  }

  .flex {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
  }

  .grid {
    display: grid;
  }

  .grid .item {
    align-self: center;
    justify-self: center;
  }



  .blue {
    color: blue;
  }

  .red {
    color: red;
  }
</style>

<body>
  <div class="continer">
    <!-- 已知宽高 absolute+margin -->
    <section class="margin1 section">
      <div class="item  red blue">111111111111</div>
    </section>
    <!-- 已知宽高,absolute 四个方向距离0，margin auto -->
    <section class="margin2 section">
      <div class="item"></div>
    </section>
    <!-- 已知宽高,absolute+transform+calc -->
    <section class="margin3 section">
      <div class="item"></div>
    </section>

    <!-- 未知宽高,absolute+transform -->
    <section class="position1 section">
      <div class="item"></div>
    </section>
    <!-- 未知宽高,inline_block+vertical-align+text-align -->
    <section class="inline_box1 section">
      <div class="item"></div>
    </section>
    <!-- 未知宽高,table-cell+vertical+center -->
    <section class="table_box1 section">
      <div class="item"></div>
    </section>
    <!-- 未知宽高,flex布局 -->
    <section class="flex section">
      <div class="item"></div>
    </section>
    <!-- 未知宽高,grid布局 -->
    <section class="grid section">
      <div class="item"></div>
    </section>
  </div>
</body>

</html>